package com.sansui.blog.utils;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;

/**
 * @name: PubClass
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-07-31 18:38
 *
 * 公用类
 **/
public class PubClass {

    //获得数字随机数
    public static String getRandomNumber(int numberCount){
        String numberStr = "";
        Random ra = new Random();
        for (int i = 0;i < numberCount;i++){
            numberStr += ra.nextInt(10)+"";
        }
        return  numberStr;
    }

    //获得字母随机数
    public static String getLetterRandomNumber(int numberCount){
        String[] number = new String[]{"0","1","2","3","4","5","6","7","8","9",
                "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
                "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        String numberStr = "";
        Random ra = new Random();
        for (int i = 0;i < numberCount;i++){
            numberStr += number[ra.nextInt(62)];
        }
        return  numberStr;
    }

    //获得限制数字大小的随机数
    public static int getRandomNumberSize(int numberLen){
        Random ra = new Random();
        return  ra.nextInt(numberLen);
    }

    //获得当前系统的年月日
    public static String getDate(){
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = new Date();
        String dates = simpleDateFormat.format(date);
        return dates;
    }

    //获得当前系统的当前秒数
    public static String getTime(){
        String pattern = "HH:mm:ss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = new Date();
        String time = simpleDateFormat.format(date);
        return time;
    }

    //获得当前系统的年月日秒数
    public static Date getDateTime(){
        String pattern = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = new Date();
        try {
            date = simpleDateFormat.parse(simpleDateFormat.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    //发送QQ邮箱
    public static void sendEmail(String email,String emailTitle,String emailValue) {
        try {
            Properties properties = new Properties();
            properties.put("mail.transport.protocol", "smtp");// 连接协议
            properties.put("mail.smtp.host", "smtp.qq.com");// 主机名
            properties.put("mail.smtp.port", 465);// 端口号
            properties.put("mail.smtp.auth", "true");
            properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用
            properties.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
            Session session = Session.getInstance(properties);
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("aixtport@qq.com"));
            message.setRecipients(Message.RecipientType.TO, new InternetAddress[]{new InternetAddress(email)});
            message.setSubject(emailTitle);
            message.setText(emailValue);

            Transport transport = session.getTransport();
            transport.connect("aixtport@qq.com", "oluecuvgzigmdfbh");// 密码为QQ邮箱开通的stmp服务后得到的客户端授权码
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
